﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Drawing;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Webkit;
using Android.Widget;
using Android.OS;

namespace MapDemo
{
    [Activity(Label = "MapDemo", MainLauncher = false , Icon = "@drawable/icon")]
    public class Activity1 : Activity
    {
        const string LocalFile = "file:///android_asset/baidumap.html";
        private WebView webView;
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            SetContentView(Resource.Layout.Main);

            webView = FindViewById<WebView>(Resource.Id.webView1);
            webView.Settings.JavaScriptEnabled = true;
            webView.AddJavascriptInterface(new JSMethod(this), "JSMethod");

            webView.LoadUrl(LocalFile);

            var btn1 = FindViewById<Button>(Resource.Id.NormalButton);
            btn1.Click += new EventHandler(btn1_Click);

            var btn2 = FindViewById<Button>(Resource.Id.NormalButton1);
            btn2.Click += new EventHandler(btn2_Click);



        }

        static List<PointF> _dataList = new List<PointF> { new PointF(116.399f, 39.910f), new PointF(116.405f, 39.920f), new PointF(116.415f, 39.928f), new PointF(116.421f, 39.915f) };
        void btn2_Click(object sender, EventArgs e)
        {
            var sb = new StringBuilder();
            sb.Append("[");
            foreach (var data in _dataList)
            {
                sb.Append("[");
                sb.Append(data.X);
                sb.Append(",");
                sb.Append(data.Y);
                sb.Append("],");
            }
            sb.Remove(sb.Length - 1, 1);
            sb.Append("]");
            var para = sb.ToString();
            InjectJavaScript("DrawLine(" + para + ");");
        }

        double a1 = 116.404;
        private double b1 = 39.915;
        void btn1_Click(object sender, EventArgs e)
        {
            InjectJavaScript("AddMark(" + a1 + "," + b1 + ");");
            //InjectJavaScript("CallMethod();");
        }

        public void InjectJavaScript(String js)
        {
            webView.LoadUrl("javascript:(function() { " + js + " })();");
        }
    }

    internal class JSMethod : Java.Lang.Object, Java.Lang.IRunnable
    {
        Context context;

        public JSMethod(Context context)
        {
            this.context = context;
        }

        public void Run()
        {
            Toast.MakeText(context, "What is the matter?", ToastLength.Short).Show();
        }
    }
}

